import Vue from 'vue'
import Vuex from 'vuex'
import { Base64 } from 'js-base64'
Vue.use(Vuex)
const store = new Vuex.Store({
	state:{
		memberType:2,
		token:"",
		swichPage:1
	},
	mutations:{
		qsLogin(state,str){
			//console.log('登录处理qsLogin,state:', state);
			//console.log('登录处理qsLogin,str:', str);
			//console.log('登录处理qsLogin,str1:', str.split(".")[1]);
			//console.log('Base64:', Base64.decode(str.split(".")[1]));
			var decodeStrBuff = new Buffer(str.split(".")[1], 'base64');
			var decodeStr = decodeStrBuff.toString('utf8'); // hex => 转成十六进制
			//console.log('decodeStr:', decodeStr);
			//let data = JSON.parse(Base64.decode(str.split(".")[1]));
			let data = JSON.parse(decodeStr);
			console.log('登录处理qsLogin,data:', data);
			state.memberType = data.utype;
			state.token = str;
			uni.setStorage({
				key: 'token',
				data: str
			})
			uni.setStorage({
				key: 'memberType',
				data: data.utype
			})
		},
		qsLogout(state){
			state.memberType = 2;
			state.token = "";
			uni.removeStorage({
				key:'token'
			})
			uni.removeStorage({
				key:'memberType'
			})
		},
		editSwichPage(state,type){
			state.swichPage = type
		}
	},
	actions:{
		qsLogin(context,str){
			context.commit("qsLogin",str)
		},
		qsLogout(context){
			context.commit("qsLogout")
		}
	}
})
export default store;